package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.BlogMapper;
import com.itheima.pojo.*;
import com.itheima.dao.BlogSortMapper;
import com.itheima.service.BlogSortService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BlogSortServiceImpl implements BlogSortService {

    @Autowired
    private BlogSortMapper blogSortMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private IdWorker idWorker;

    //1 增加博客分类
    @Override
    public void add(BlogSort blogSort) {
        blogSort.setUid(idWorker.nextId()+"");
        blogSort.setStatus(1);
        blogSortMapper.insert(blogSort);
    }

    //2 批量删除博客分类
    @Override
    public Result deleteBatch(List<BlogSortVO> list) {

        for (BlogSortVO blogSortVO : list) {
            String uid = blogSortVO.getUid();
            //根据博客分类的uid查询该分类是否有博客
            Blog blog = new Blog();
            blog.setBlogSortUid(uid);
            Blog blog1 = blogMapper.selectOne(blog);
            if (blog1!=null){
                return new Result(StatusCode.ERROR, "该分类下还有博客");
            }
            blogSortMapper.deleteByPrimaryKey(uid);
        }

        return new Result(StatusCode.OK, "删除成功");
    }

    //3 编辑博客分类
    @Override
    public void edit(BlogSort blogSort) {
     blogSortMapper.updateByPrimaryKeySelective(blogSort);
    }

    //4  获取博客分类列表
    @Override
    public Page<BlogSort> getList(BlogSortVO blogSortVO) {
        PageHelper.startPage( blogSortVO.getCurrentPage(),blogSortVO.getPageSize());
        Example example = createExample(blogSortVO);
        return (Page<BlogSort>)blogSortMapper.selectByExample(example);
    }

    //7 通过点击量排序博客分类
    @Override
    public void blogSortByClickCount() {
        int i = blogSortMapper.blogSortByClickCount();
        if (i>0){
            System.out.println("操作成功！");
        }
    }

    //6 通过引用量排序博客分类
    @Override
    public void blogSortByCite() {
        int i = blogSortMapper.blogSortByCite();
        if (i>0){
            System.out.println("操作成功！");
        }
    }

    //5  置顶分类
    @Override
    public void stick(BlogSortVO blogSortVO) {

        BlogSort blogSort = new BlogSort();
        int maxSort = blogSortMapper.findMaxSort();
        String uid = blogSortVO.getUid();
        BlogSort blogSort1 = new BlogSort();
        blogSort1.setUid(uid);
        BlogSort blogSort2 = blogSortMapper.selectOne(blogSort1);

        //设置当前博客分类的sort值比最大值多1
        blogSort.setSort(maxSort+1);
        blogSort.setUid(blogSortVO.getUid());
        blogSort.setSortName(blogSort2.getSortName());
        blogSort.setContent(blogSort2.getContent());
        blogSort.setClickCount(blogSort2.getClickCount());
        blogSort.setStatus(1);
        blogSortMapper.updateByPrimaryKey(blogSort);

    }

    /**
     * 根据uid查询对象
     * @param uid
     * @return
     */
    @Override
    public BlogSort findBlogByUid(String uid) {
        return blogSortMapper.getBlogSortByUid(uid);
    }

    /**
     * 获取所有分类信息
     * @return
     */
    @Override
    public List<BlogSort> getAllBlogSort() {
        return blogSortMapper.selectAll();
    }


    /*
     * 封装查询条件对象*/
    private Example createExample(BlogSortVO blogSortVO) {
        Example example = new Example(BlogSort.class);
        example.setOrderByClause("sort DESC");
        Example.Criteria criteria = example.createCriteria();
        if (blogSortVO!= null) {
            // 根据分类名称模糊查询
            if(blogSortVO.getKeyword()!=null && !"".equals(blogSortVO.getKeyword())){
                criteria.andLike("sortName","%"+blogSortVO.getKeyword()+"%");
            }
        }
        return example;
    }

}
